<?php


function generateQuestions ($questionArrayInformation, $tableName) {
			
	$divHTML = generatePrimaryDiv();
	
	switch ($questionArrayInformation['type']) {
		case 1: //drop down menu
			return generateDropDownMenu($divHTML, $questionArrayInformation, $tableName);
			break;
		case 2: //text input
			return generateTextInput($divHTML, $questionArrayInformation, $divNames);
			break;
		case 3: //date
			return generateDateInput($divHTML, $questionArrayInformation, $divNames);
			break;
		case 4: //medication
			return generateMedicationDropDownMenu($divHTML, $questionArrayInformation, $divNames);
			break;
		case 5: //medical test
			return generateTestDropDown($divHTML, $questionArrayInformation);
			break;
		case 6:
			return '';
			break;
	}
	
}


function generatePrimaryDiv () { //generates first div with question text
	
	return '<div class="answerLi2">';
}


function generateDateInput($divHTML, $questionArray, $divNames) {
	
	
	//generate date div
	{
		$divHTML .= '<div id="dateInput_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		
		if (isset($_POST['datePick_type' . $questionArray['type'] . '_num' . $questionArray['qnumber']])) {
			$ans = $_POST['datePick_type' . $questionArray['type'] . '_num' . $questionArray['qnumber']];
			$divHTML .= '<input type="text" id="datePickid_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" name="datePick_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . 
			'" value="' . $ans . '" style="text-align: center;">';
		} else {
			$divHTML .= '<input type="text" id="datePickid_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" name="datePick_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		}
		
		
		$divHTML .= '<script type="text/javascript">
	
						$("#datePickid_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '").datepicker({ minDate: new Date(1950, 01, 01), showOn: "both", dateFormat: "yy-mm-dd"});
						
					</script>';
	}
	
	
	//adding end of fields
	{
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	
	//value to return
	{
		$returnArray = array();

		$returnArray[] = array("textId" => "text_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"answerName" => "datePick_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"type" => 3); // request name and select ID
		
		$returnArray[] = $divHTML;//return HTML to be rendered by webpage
			
	}
	
	return $returnArray;
}





function generateTextInput($divHTML, $questionArray, $divNames) {
	
	//text input
	{
		$divHTML .= '<div id="textInput_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		
		if (isset($_POST["textInputAns_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']])) {
			$ans = $_POST["textInputAns_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']];
		}
		
		if (isset($ans)) {
			$divHTML .= '<input type="text" class="textInput" name="textInputAns_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" value="' . $ans . '">';
		} else {
			$divHTML .= '<input type="text" class="textInput" name="textInputAns_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		}
	}
	
	//adding end of fields
	{
		$divHTML .= '</select>'; //end of drop down menu
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	//value to return
	{
		$returnArray = array();

		$returnArray[] = array("textId" => "text_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"answerName" => "textInputAns_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"type" => 2); // request name and select ID
		
		$returnArray[] = $divHTML;//return HTML to be rendered by webpage
			
	}
	
	return $returnArray;
}






function generateDropDownMenu ($divHTML, $questionArray, $tableName) {
	
	//beginning dropdown elements
	{
		$divHTML .= 	'<select name="' . $tableName . '_num_' . $questionArray['qnumber'] . '" style="margin-bottom: 10px; max-width: 200px;">';
		$divHTML .= 		'<option value="E"> - Either- </option>';
	}
	
	#return print_r($questionArray['answer_options'], true);
	
	
	//fill select drop down
	foreach ($questionArray['answer_options'] as $key => $value) {
		
		if (isset($_POST[$tableName . '_num_' . $questionArray['qnumber']]))	
			$ans = $_POST[$tableName . '_num_' . $questionArray['qnumber']];
		
		if (isset($ans) && $ans == $value['type']) 
			{
				$divHTML .= '<option value="' . $value['type'] . '" selected="selected">' . $value['type'] . '</option>';
					
			} else {
				
				$divHTML .= '<option value="' . $value['type'] . '">' . $value['type'] . '</option>';
			}
	}
	
	//adding end of fields
	{
		$divHTML .= '</select>'; //end of drop down menu
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	return $divHTML;
}




//generates drop down menu containing all medication names. Returns an array containing all text IDs and select names
function generateMedicationDropDownMenu ($divHTML, $questionArray, $divNames) {
	
	//query
	{
		$conn = GetConnection();	
		$sql = "SELECT * FROM Medication ORDER BY Name";
		$sqlResults = $conn->query($sql);
	}
	
	//beginning div and select elements
	{
		$divHTML .= '<div id="medDropDown_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		$divHTML .= 	'<select name="medSelect_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" style="margin-bottom: 10px;">';
		$divHTML .= 		'<option value="9999"> - Treatment Name - </option>';
	}
	
	
	//fill select drop down
	while($medicationInfo = $sqlResults->fetch_assoc()) {
		
	if (isset($_POST["medSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']]))
		$ans = $_POST["medSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']];
	 
	if (isset($ans) && $ans == $medicationInfo['ID']) 
		{
			$divHTML .= '<option value="' . $medicationInfo['ID'] . '" selected="selected">' . $medicationInfo['Name'] . '</option>';
				
		} else {
			
			$divHTML .= '<option value="' . $medicationInfo['ID'] . '">' . $medicationInfo['Name'] . '</option>';
		}
	}
	
	//adding end of fields
	{
		$divHTML .= '</select>'; //end of drop down menu
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	return $divHTML;
}


function generateTestDropDown($divHTML, $questionArray) {
	
	//query
	{
		$conn = GetConnection();	
		$sql = "SELECT * FROM Test ORDER BY ID";
		$sqlResults = $conn->query($sql);
	}
	
	//beginning div and select elements
	{
		$divHTML .= '<div id="testDropDown_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		$divHTML .= 	'<select name="testSelect_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" style="margin-bottom: 10px; width: 200px;">';
		$divHTML .= 		'<option value="9999"> - Test Name - </option>';
	}
	
	
	//fill select drop down
	while($testInfo = $sqlResults->fetch_assoc()) {
		
	if (isset($_POST["testSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']])) {
		$ans = $_POST["testSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']];
	}	
	 
	if (isset($ans) && $ans == $testInfo['ID']) 
		{
			$divHTML .= '<option value="' . $testInfo['ID'] . '" selected="selected">' . $testInfo['Name'] . '</option>';
				
		} else {
			
			$divHTML .= '<option value="' . $testInfo['ID'] . '">' . $testInfo['Name'] . '</option>';
		}
	}
	
	//adding end of fields
	{
		$divHTML .= '</select>'; //end of drop down menu
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	return $divHTML;
}